package org.example;

import org.example.admin.admin.ShowAdminInfoView;
import org.example.admin.aircraft.ShowAircraftInfoView;
import org.example.admin.airway.ShowAirwayInfoView;
import org.example.admin.flight.ShowFlightInfoView;
import org.example.common.Mysql;
import org.example.admin.order.ShowOrderInfoView;
import org.example.user.flight.ShowUserFlightInfoView;
import org.example.user.order.ShowUserOrderInfoView;
import org.example.user.user.ShowUserInfoView1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * 登录界面(管理员和用户)
 */
public class LoginPage extends JFrame {
    private JFrame frame;
    private JPanel loginPanel;
    private JPanel adminPanel, userPanel;
    private JLabel messageLabel;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JComboBox<String> identityComboBox;
    public static String currentUserName, username;
    public static int currentUserId;

    public LoginPage() {
        frame = new JFrame("登录页面");
        loginPanel = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(10, 10, 10, 10);

        JLabel identityLabel = new JLabel("身份: ");
        loginPanel.add(identityLabel, c);

        c.gridx = 1;
        //创建身份选择选框
        String[] identities = {"管理员", "用户"};
        identityComboBox = new JComboBox<>(identities);
        loginPanel.add(identityComboBox, c);

        c.gridx = 0;
        c.gridy = 1;

        JLabel usernameLabel = new JLabel("用户名: ");
        usernameField = new JTextField(20);
        loginPanel.add(usernameLabel, c);

        c.gridx = 1;
        loginPanel.add(usernameField, c);

        c.gridx = 0;
        c.gridy = 2;
        JLabel passwordLabel = new JLabel("密码: ");
        passwordField = new JPasswordField(20);
        loginPanel.add(passwordLabel, c);

        c.gridx = 1;
        loginPanel.add(passwordField, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.CENTER;

        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //提交的数据获取
                String identity = (String) identityComboBox.getSelectedItem();
                username = usernameField.getText();

                currentUserName = username;

                String password = new String(passwordField.getPassword());

                boolean isValid = false;
                try {
                    isValid = performLoginValidation(identity, username, password);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }

                if (isValid && identity.equals("管理员")) {
                    showAdminPage(identity, username);
                } else if (isValid && identity.equals("用户")) {
                    try {
                        showUserPage(identity, username);
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                } else {
                    showMessage("登录失败，请检查用户名和密码。");
                }
            }
        });
        loginPanel.add(loginButton, c);

        c.gridy = 4;
        messageLabel = new JLabel();
        loginPanel.add(messageLabel, c);

        frame.add(loginPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(600, 400);//尺寸

        frame.setVisible(true);//设置可视化面板
        frame.setLocationRelativeTo(null);
    }

    private boolean performLoginValidation(String identity, String username, String password) throws SQLException, ClassNotFoundException {

        //声明一个自定义的mysql对象,进行登录前的身份数据库查询与校验
        Mysql mysql = new Mysql();
        boolean adminFlag = mysql.adminSearch(username, password);//管理员身份验证
        boolean userFlag = mysql.userSearch(username, password);//用户身份验证

        if (identity.equals("管理员") && adminFlag == true) {
            return true;
        } else return identity.equals("用户") && userFlag == true;
    }

    /**
     * 管理员登录成功后的界面显示
     *
     * @param identity
     * @param username
     */
    private void showAdminPage(String identity, String username) {
        frame.getContentPane().remove(loginPanel); //登录页面的消除

        adminPanel = new JPanel();
        JLabel welcomeLabel = new JLabel();

        if (identity.equals("管理员")) {
            welcomeLabel.setText("欢迎管理员 " + username + " 登录！");

        } else if (identity.equals("用户")) {
            welcomeLabel.setText("欢迎用户 " + username + " 登录！");
        }
        adminPanel.add(welcomeLabel);


        //信息查询按钮
        JButton button1 = new JButton("飞机");
        JButton button2 = new JButton("航线");
        JButton button3 = new JButton("航班");
        JButton button4 = new JButton("订单");
        JButton button5 = new JButton("管理员个人信息");
        JButton button6 = new JButton("所有用户信息");

        //按钮监听事件
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowAircraftInfoView showAircraftInfoForm = new ShowAircraftInfoView();
                    showAircraftInfoForm.setSize(400, 300);
                    showAircraftInfoForm.setVisible(true);
                });
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowAirwayInfoView ShowAirwayInfoForm = new ShowAirwayInfoView();
                    ShowAirwayInfoForm.setSize(400, 300);
                    ShowAirwayInfoForm.setVisible(true);
                });
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowFlightInfoView ShowFlightInfoForm = new ShowFlightInfoView();
                    ShowFlightInfoForm.setSize(1200, 300);
                    ShowFlightInfoForm.setVisible(true);
                });
            }
        });
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowOrderInfoView ShowOrderInfoForm = new ShowOrderInfoView();
                    ShowOrderInfoForm.setSize(1300, 300);
                    ShowOrderInfoForm.setVisible(true);
                });
            }
        });
        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowAdminInfoView showAdminInfoForm = new ShowAdminInfoView();
                    showAdminInfoForm.setSize(400, 300);
                    showAdminInfoForm.setVisible(true);
                });
            }
        });
        button6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    org.example.admin.user.ShowUserInfoView showUserInfoForm = new org.example.admin.user.ShowUserInfoView();
                    showUserInfoForm.setSize(800, 300);
                    showUserInfoForm.setVisible(true);
                });
            }
        });

        adminPanel.add(button1);
        adminPanel.add(button2);
        adminPanel.add(button3);
        adminPanel.add(button4);
        adminPanel.add(button5);
        adminPanel.add(button6);

        frame.getContentPane().add(adminPanel);

        frame.invalidate();
        frame.validate();
        frame.repaint();

        frame.setLocationRelativeTo(null);
    }


    /**
     * 用户登录成功后的界面显示
     *
     * @param identity
     * @param username
     */
    private void showUserPage(String identity, String username) throws SQLException, ClassNotFoundException {
        frame.getContentPane().remove(loginPanel);

        //获取当前登录用户的id
        Mysql mysql = new Mysql();
        currentUserId = mysql.getCurrentUserId();
        userPanel = new JPanel();
        JLabel welcomeLabel = new JLabel();

        if (identity.equals("管理员")) {
            welcomeLabel.setText("欢迎管理员 " + username + " 登录！");

        } else if (identity.equals("用户")) {
            welcomeLabel.setText("欢迎用户 " + username + " 登录！");
        }
        userPanel.add(welcomeLabel);

        //按钮添加
        JButton button1 = new JButton("现有航班");
        JButton button2 = new JButton("个人订单");
        JButton button3 = new JButton("个人信息");

        //按钮监听事件
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowUserFlightInfoView showUserFlightInfoForm = new ShowUserFlightInfoView();
                    showUserFlightInfoForm.setSize(1600, 300);
                    showUserFlightInfoForm.setVisible(true);
                });
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowUserOrderInfoView showUserOrderInfoForm = new ShowUserOrderInfoView();
                    showUserOrderInfoForm.setSize(1400, 300);
                    showUserOrderInfoForm.setVisible(true);
                });
            }
        });
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
////                frame.dispose();
                SwingUtilities.invokeLater(() -> {
                    ShowUserInfoView1 showUserInfo1From = new ShowUserInfoView1();
                    showUserInfo1From.setSize(400, 300);
                    showUserInfo1From.setVisible(true);
                });
            }
        });


        userPanel.add(button1);
        userPanel.add(button2);
        userPanel.add(button3);

        frame.getContentPane().add(userPanel);
        frame.invalidate();
        frame.validate();
        frame.repaint();
        frame.setLocationRelativeTo(null);
    }


    private void showMessage(String message) {
        messageLabel.setText(message);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LoginPage();
            }
        });
    }
}

